package com.hdu.other.oneDimensionPreInfo;

import java.util.HashMap;
import java.util.Map;

public class lc560 {

    class Solution {
        public int subarraySum(int[] nums, int k) {
             Map<Integer, Integer> preSum2count = new HashMap<>();
             preSum2count.put(0, 1);
             int ans = 0;
             int preSum = 0;
             for (int i = 0; i < nums.length; i++) {
                 preSum += nums[i];
                 if (preSum2count.containsKey(preSum - k)) {
                     ans += preSum2count.get(preSum - k);
                 }
                 preSum2count.put(preSum, preSum2count.getOrDefault(preSum, 0) + 1);
             }
             return ans;
        }
    }

}
